package com.waidi.clothespad.ui;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.waidi.clothespad.R;
import com.waidi.clothespad.common.Config;
import com.waidi.clothespad.common.DpsInterface;

import com.waidi.clothespad.common.HttpGetThread;
import com.waidi.clothespad.common.MessageToUi;
import com.waidi.clothespad.common.MyToast;

import java.net.URLEncoder;

/**
 * Created by Administrator on 2019/6/26.
 */

public class QrCodeBoundDialog extends Dialog{
    private View view;
    private static final String TAG = "QrCodeBoundDialog";
    private Context context;
    private Handler handler0;

    private  QrCodeBoundDialogHandler selfHandler;
    private TextView tV_qrCode_id;
    private TextView tV_order_id;
    private TextView tV_style_id;
    private TextView tV_style_name;

    private TextView tV_color;
    private TextView tV_size;

    private TextView tV_line_no;
    private TextView tV_hanger;
    private TextView tV_num;







    private Button bt_ok;
    private Button bt_cancle;



    public String cutPlanId ;

    public static String title0 = "请刷物料卡...";
    public static String partName = "";
    public static String partCode = "";


    public QrCodeBoundDialog(@NonNull Context context,Handler handlerin) {
        super(context);
        this.context = context;
        this.handler0 = handlerin;
        selfHandler = new QrCodeBoundDialogHandler();
        init();
    }
    public QrCodeBoundDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //init();
    }

    private void init() {

        View view = LayoutInflater.from(context).inflate(R.layout.layer_qrcode_bound_info ,null);
        tV_qrCode_id = (TextView) view.findViewById(R.id.tv_qrcode);
        tV_order_id = (TextView) view.findViewById(R.id.tv_order_id);
        tV_style_id = (TextView) view.findViewById(R.id.tv_style_id);

        tV_style_name = (TextView) view.findViewById(R.id.tv_style_name);

        tV_color = (TextView) view.findViewById(R.id.tv_color);
        tV_size = (TextView) view.findViewById(R.id.tv_size);
        tV_line_no = (TextView) view.findViewById(R.id.tv_line_no);
        tV_num = (TextView) view.findViewById(R.id.tv_num);
        tV_hanger = (TextView) view.findViewById(R.id.tv_hanger);

        bt_ok = (Button) view.findViewById(R.id.bt_ok);
        bt_cancle = (Button) view.findViewById(R.id.bt_cancle);




        bt_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                dismiss();
            }
        });

        bt_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

            try
            {

                    String qrCode  = tV_qrCode_id.getText().toString();
                    String colorName = tV_color.getText().toString();
                    String colorCode = tV_color.getText().toString();

                    String styleCode = tV_style_id.getText().toString();
                    String lineNo = tV_line_no.getText().toString();
                    String orderNum = tV_num.getText().toString();
                    String orderCode = tV_order_id.getText().toString();
                    String sizeCode = tV_size.getText().toString();
                    String hanger = tV_hanger.getText().toString();

                    qrCode =  URLEncoder.encode(qrCode, "utf-8");

                    String url = DpsInterface.qRCodeBound(qrCode,orderCode,lineNo,Config.rfidWorkerInfo.workernum,hanger,sizeCode,Config.qrcode);

                    Log.i(TAG,url);
                    String arg[] = new String[10];
                    HttpGetThread thread = new HttpGetThread(selfHandler, context, url, 0, "quickOnLineByCard", arg);
                    thread.start();
                    dismiss();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

        setContentView(view);
        this.getWindow().setDimAmount(0.1f);
        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.45); // 宽度设置为屏幕的0.65，根据实际情况调整
        params.height = (int) (d.getHeight() * 0.75); // 宽度设置为屏幕的0.65，根据实际情况调整
        dialogWindow.setAttributes(params);
    }

    public void iniData()
    {

        //使dialog失去焦点 使键盘事件能够一直停留在主Activity 上
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        this.setCanceledOnTouchOutside(true);
    }





    public void refushData(String qrCode,String orderId,String styleID,String styleName,
                           String colorId,String sizeId,String lineNum,String hanger,
                           String orderNum)
    {

        tV_qrCode_id.setText(qrCode);
        tV_order_id.setText(orderId);
        tV_style_id.setText(styleID);
        tV_style_name.setText(styleName);
        tV_color .setText(colorId);
        tV_size.setText(sizeId);
        tV_line_no.setText(lineNum);
        tV_num .setText(orderNum);
        tV_hanger.setText(hanger);


        //使dialog失去焦点
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        this.setCanceledOnTouchOutside(true);


    }




/*    String barcode ="";
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        String code;
        Log.i(TAG,"##"+event.toString());
        {
            if (event.getAction() != KeyEvent.ACTION_DOWN)
            {
                Log.i(TAG, "dispatchKeyEvent: " + event.toString());
                char pressedKey = (char) event.getUnicodeChar();
                barcode += pressedKey;
            }
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                Toast.makeText(context,
                        "barcode--->>" + barcode, Toast.LENGTH_SHORT)
                        .show();

                // qrEditText.setText(barcode);
                //MyToast.showToast(context,barcode);
                Log.i(TAG, "BARCODE:" + barcode);
                code = barcode;
                tV_qrCode_id.setText(barcode);
               // new MessageToUi().send(900, "qrcode", code, Config.fragShishishuju.handler);
                barcode = "";
            }


        }
        return super.dispatchKeyEvent(event);
    }*/


    //自定义handler类
    class QrCodeBoundDialogHandler extends Handler
    {
        @Override
        //接收别的线程的信息并处理
        public void handleMessage(Message msg) {
            Log.i(TAG,Integer.toString(msg.arg1));
            Bundle bundle;
            try {
                    switch (msg.arg1)
                    {
                        case 0:
                        {
                            bundle = msg.getData();
                            String ss = bundle.getString("data");
                            MyToast.showToast(context,ss);
                        }
                        break;
                        case 1:
                        {


                        }
                        break;
                        case 2:

                            break;
                        case 23:
                        {
                            Bundle bundle23=msg.getData();
                            String exception = bundle23.getString("exception");
                            MyToast.showToast(context,exception);
                        }
                        break;
                        case 444:
                        {
                            Bundle bundle444=msg.getData();
                            String info = bundle444.getString("exception");
                            MyToast.showToast(context,info);

                        }
                        break;

                        default:
                            break;
                }
            }catch (Exception e)
            {
                e.printStackTrace();
                MyToast.showToast(context,e.toString());
            }
        }
    }

}
